package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入类名：");
        String className = scanner.nextLine();
        System.out.println("输入方法名：");
        String methodName = scanner.nextLine();

        //1.1通过类对象获取要实例化的类的类对象
        Class cls = Class.forName(className);
        //1.2通过类对象，实例化需要的类
        Object obj = cls.newInstance();

        //2.1通过类对象获取要调用的方法的方法对象
        Method method = cls.getMethod(methodName);
        //2.2通过方法对象来调用该方法
        method.invoke(obj);
    }
}
